import { login } from '@/api/auth'
import { getToken, setToken, removeToken, getTokenR, setTokenR, removeTokenR, getUsername, setUsername, removeUsername, getId, setId, removeId, getDept, setDept, removeDept, getTel, setTel, removeTel, getEmail, setEmail, removeEmail, getPassword, setPassword, removePassword, getFaceuri, setFaceuri, removeFaceuri } from '@/utils/auth'
import { Notification } from 'element-ui'

const user = {
  state: {
    token: getToken() || getTokenR(),
    username: getUsername(),
    id: getId(),
    dept: getDept(),
    tel: getTel(),
    email: getEmail(),
    password: getPassword(),
    faceuri: getFaceuri()
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_USERNAME: (state, username) => {
      state.username = username
    },
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_DEPT: (state, dept) => {
      state.dept = dept
    },
    SET_TEL: (state, tel) => {
      state.tel = tel
    },
    SET_EMAIL: (state, email) => {
      state.email = email
    },
    SET_PASSWORD: (state, password) => {
      state.password = password
    },
    SET_FACEURI: (state, faceuri) => {
      state.faceuri = faceuri
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const id = userInfo.id.trim()
      return new Promise((resolve, reject) => {
        login(id, userInfo.password).then(res => {
          if (res.data.code === 1) {
            Notification({
              title: '登录失败',
              message: res.data.msg,
              type: 'error',
              duration: 3000
            })
          } else {
            const data = res.data.data
            commit('SET_TOKEN', data.Authorization)
            commit('SET_USERNAME', data.user.name)
            commit('SET_ID', data.user.id)
            if (data.user.dept) {
              commit('SET_DEPT', data.user.dept.name)
            }
            commit('SET_TEL', data.user.tel)
            commit('SET_EMAIL', data.user.email)
            commit('SET_PASSWORD', '********')
            commit('SET_FACEURI', data.user.faceuri)
            setToken(data.Authorization)
            setUsername(data.user.name)
            setId(data.user.id)
            if (data.user.dept) {
              setDept(data.user.dept.name)
            }
            setTel(data.user.tel)
            setEmail(data.user.email)
            setPassword('********')
            setFaceuri(data.user.faceuri)
            resolve()
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    LoginRemember({ commit }, userInfo) {
      const id = userInfo.id.trim()
      return new Promise((resolve, reject) => {
        login(id, userInfo.password).then(res => {
          if (res.data.code === 1) {
            Notification({
              title: '登录失败',
              message: res.data.msg,
              type: 'error',
              duration: 3000
            })
          } else {
            const data = res.data.data
            commit('SET_TOKEN', data.Authorization)
            commit('SET_USERNAME', data.user.name)
            commit('SET_ID', data.user.id)
            if (data.user.dept) {
              commit('SET_DEPT', data.user.dept.name)
            }
            commit('SET_TEL', data.user.tel)
            commit('SET_EMAIL', data.user.email)
            commit('SET_PASSWORD', '********')
            commit('SET_FACEURI', data.user.faceuri)
            setTokenR(data.Authorization)
            setUsername(data.user.name)
            setId(data.user.id)
            if (data.user.dept) {
              setDept(data.user.dept.name)
            }
            setTel(data.user.tel)
            setEmail(data.user.email)
            setPassword('********')
            setFaceuri(data.user.faceuri)
            resolve()
          }
        }).catch(err => {
          reject(err)
        })
      })
    },

    // 登出
    /* LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout().then(() => {
          commit('SET_TOKEN', '')
          commit('SET_USERNAME', '')
          removeToken()
          removeUsername()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    }, */

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        commit('SET_USERNAME', '')
        commit('SET_ID', '')
        commit('SET_DEPT', '')
        commit('SET_TEL', '')
        commit('SET_EMAIL', '')
        commit('SET_PASSWORD', '')
        removeToken()
        removeTokenR()
        removeUsername()
        removeId()
        removeDept()
        removeTel()
        removeEmail()
        removePassword()
        removeFaceuri()
        resolve()
      })
    }
  }
}

export default user
